package com.itx.gatewayx.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.NumberUtil;
import cn.hutool.system.RuntimeInfo;
import cn.hutool.system.SystemUtil;
import com.itx.gatewayx.common.model.ResponseResult;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.lang.management.ManagementFactory;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 系统监控控制器
 *
 * @author itx
 */
@Slf4j
@RestController
@RequestMapping("/monitor")
public class MonitorController {

    /**
     * 获取服务器信息
     *
     * @return 服务器信息
     */
    @GetMapping("/server")
    public Mono<ResponseResult<Map<String, Object>>> getServerInfo() {
        Map<String, Object> resultMap = new HashMap<>();
        
        try {
            // 系统信息
            Map<String, Object> sysInfo = new HashMap<>();
            sysInfo.put("computerName", SystemUtil.getHostInfo().getName());
            sysInfo.put("computerIp", SystemUtil.getHostInfo().getAddress());
            sysInfo.put("osName", SystemUtil.getOsInfo().getName());
            sysInfo.put("osArch", SystemUtil.getOsInfo().getArch());
            sysInfo.put("osVersion", SystemUtil.getOsInfo().getVersion());
            resultMap.put("sys", sysInfo);
            
            // JVM信息
            Map<String, Object> jvmInfo = new HashMap<>();
            jvmInfo.put("jvmName", SystemUtil.getJvmInfo().getName());
            jvmInfo.put("javaVersion", System.getProperty("java.version"));
            jvmInfo.put("javaHome", System.getProperty("java.home"));
            
            // JVM启动时间
            long startTime = ManagementFactory.getRuntimeMXBean().getStartTime();
            jvmInfo.put("startTime", DateUtil.format(new Date(startTime), "yyyy-MM-dd HH:mm:ss"));
            
            // JVM运行时间
            long runTime = System.currentTimeMillis() - startTime;
            jvmInfo.put("runTime", DateUtil.formatBetween(runTime));
            
            // JVM内存信息
            RuntimeInfo runtimeInfo = SystemUtil.getRuntimeInfo();
            jvmInfo.put("maxMemory", NumberUtil.div(runtimeInfo.getMaxMemory(), 1024 * 1024, 2) + " MB");
            jvmInfo.put("totalMemory", NumberUtil.div(runtimeInfo.getTotalMemory(), 1024 * 1024, 2) + " MB");
            jvmInfo.put("freeMemory", NumberUtil.div(runtimeInfo.getFreeMemory(), 1024 * 1024, 2) + " MB");
            jvmInfo.put("usedMemory", NumberUtil.div(runtimeInfo.getUsableMemory(), 1024 * 1024, 2) + " MB");
            
            double memoryUsage = NumberUtil.div(runtimeInfo.getUsableMemory(), runtimeInfo.getTotalMemory(), 4) * 100;
            jvmInfo.put("memoryUsage", NumberUtil.round(memoryUsage, 2) + "%");
            resultMap.put("jvm", jvmInfo);
            
            // CPU信息
            Map<String, Object> cpuInfo = new HashMap<>();
            cpuInfo.put("cpuNum", Runtime.getRuntime().availableProcessors());
            resultMap.put("cpu", cpuInfo);
            
            // 内存信息
            Map<String, Object> memoryInfo = new HashMap<>();
            Runtime runtime = Runtime.getRuntime();
            long totalMemory = runtime.totalMemory();
            long freeMemory = runtime.freeMemory();
            long maxMemory = runtime.maxMemory();
            long usedMemory = totalMemory - freeMemory;

            memoryInfo.put("total", NumberUtil.div(totalMemory, 1024 * 1024 * 1024, 2) + " GB");
            memoryInfo.put("used", NumberUtil.div(usedMemory, 1024 * 1024 * 1024, 2) + " GB");
            memoryInfo.put("free", NumberUtil.div(freeMemory, 1024 * 1024 * 1024, 2) + " GB");

            double memUsage = NumberUtil.div(usedMemory, totalMemory, 4) * 100;
            memoryInfo.put("usage", NumberUtil.round(memUsage, 2) + "%");
            resultMap.put("memory", memoryInfo);
            
            return Mono.just(ResponseResult.success(resultMap));
        } catch (Exception e) {
            log.error("获取服务器信息异常", e);
            return Mono.just(ResponseResult.error("获取服务器信息异常"));
        }
    }
} 